package com.example.demo.controller;

import com.example.demo.entity.Outstorage;
import com.example.demo.service.OutstorageService;
import com.example.demo.util.RestBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/outstorages")

public class OutstorageController {

    @Autowired
    private OutstorageService outstorageService;

    @PostMapping
    public RestBean<Integer> createOutstorage(@RequestBody Outstorage outstorage) {
        try {
            int result = outstorageService.createOutstorage(outstorage);
            return result > 0 ?
                    RestBean.success(result, "创建成功") :
                    RestBean.failure(400, "创建失败");
        } catch (Exception e) {
            return RestBean.failure(400, e.getMessage());
        }
    }

    @GetMapping("/storage/{sid}")
    public RestBean<List<Outstorage>> getByStorage(@PathVariable Integer sid) {
        return RestBean.success(outstorageService.findOutstoragesByStorageId(sid));
    }
}